#! /usr/bin/python
from collections import defaultdict
import os
import scipy.io
from projects.electrophysiology.view_data.experiment import Experiment
from scipy import array
import sys
import numpy
import pylab

import os
# loop throught hte experiments of interest and load the data files that will be used in the raster plot

expe_min = 832
expe_max = 855
excluded_list = [839]
stim_type = 'scanning_dot'
experiments = [Experiment.from_default_db(index) for index in 
               range(expe_min,expe_max+1)]

exps = [e for e in experiments if e.stim_type == stim_type and 
                                  e.expe_id not in excluded_list]

#now create a dictionary where angle is the key and the values are list of experiments with that angle
angle_sorted_exps = defaultdict(list)
angle_sorted_ids = defaultdict(list)
angle_sorted_results = {}
for e in exps:
    angle = e.get_setting('angle')
    angle_sorted_exps[angle].append(e)
    angle_sorted_ids[angle].append(e.expe_id)

#load one raw voltage trace to get the total time
total_time = 100000
print total_time
file_names_list = []
count_list = []
all_spikes = []
count=1
#loop through our id dictionary and load the files which are now in the correct angle order
for angle in sorted(angle_sorted_ids.keys()):
    expe_id_list = angle_sorted_ids[angle]
    for e in expe_id_list:
        spike_xs = numpy.loadtxt('%s-Turtle_Cortex_II-data-detection.txt' % e)
        all_spikes.extend(spike_xs)
        spike_ys = numpy.ones(len(spike_xs))*(count+1)
        pylab.plot(spike_xs, spike_ys, linewidth=0, marker='|', markersize=8,
                   markeredgewidth=0.5, color='k')
        count += 1
        count_list.append(count)
        file_names_list.append(e)
pylab.ylim(1,count+1)
pylab.xlim(0,total_time)
ax = pylab.gca()
pylab.xlabel('time (ms)')
pylab.ylabel('trial #')
ax.set_yticks(count_list)
ax.set_yticklabels(file_names_list)
pylab.show()
